package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.druid.sql.visitor.functions.Char;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.mapper.CartMapper;
import com.itheima.reggie.service.CartService;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;

    @Override
    public Cart addCart(Cart cart) {
        //判断当前菜品或者套餐是否在表中存在
        //select * from shopping_cart where dish_id = #{菜品id} and userId =#{登录用户id}
        //select * from shopping_cart where setmeal_id = #{套餐id} and userId =#{登录用户id}
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(cart.getDishId() != null, Cart::getDishId, cart.getDishId())
                .eq(cart.getSetmealId() != null, Cart::getSetmealId, cart.getSetmealId())
                .eq(Cart::getUserId, UserHolder.get().getId());
        Cart selectOne = cartMapper.selectOne(wrapper);
        //2.如果存在，说明已经添加过只需将数据库中查询的number+1，更新回数据库
        if (selectOne != null) {
            selectOne.setNumber(selectOne.getNumber() + 1);
            cartMapper.updateById(selectOne);
            return selectOne;
        } else {
            //3.如果不存在，说明是第一次添加，补齐参数，返回数据库
            cart.setUserId(UserHolder.get().getId());
            cart.setNumber(1);
            cart.setCreateTime(new Date());
            cartMapper.insert(cart);
            return cart;
        }
    }

    @Override
    public List<Cart> findByCart() {
        //select * from shopping_cart where user_id = #{登录用户id}
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId, UserHolder.get().getId());
        return cartMapper.selectList(wrapper);
    }

    @Override
    public Cart subNum(Cart cart) {
        //select * from shopping_cart where dish_id = #{菜品id} and userId =#{登录用户id}
        //select * from shopping_cart where setmeal_id = #{套餐id} and userId =#{登录用户id}
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(cart.getDishId() != null, Cart::getDishId, cart.getDishId())
                .eq(cart.getSetmealId() != null, Cart::getSetmealId, cart.getSetmealId())
                .eq(Cart::getUserId, UserHolder.get().getId());
        Cart cart1 = cartMapper.selectOne(wrapper);
        if (cart1 != null) {
            cart1.setNumber(cart1.getNumber() - 1);
            if (cart1.getNumber() <= 0) {
                //删除
                cartMapper.deleteById(cart1.getId());
            } else {
                //更新
                cartMapper.updateById(cart1);
            }
        }
        return cart;
    }

    @Override
    public void clean() {
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId, UserHolder.get().getId());
        cartMapper.delete(wrapper);

    }
}